package com.kongfuzi.student.ui.gaspay;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.kongfuzi.student.R;
import com.kongfuzi.student.bean.MessageEvent;
import com.kongfuzi.student.support.utils.PayResult;
import com.kongfuzi.student.app.GasApplication;
import com.kongfuzi.student.support.utils.LogUtil;
import com.kongfuzi.student.support.utils.PayUtils;
import com.kongfuzi.student.support.utils.ToastUtil;
import com.kongfuzi.student.support.utils.Util;
import com.kongfuzi.student.ui.base.CustomerActionBarActivity;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;

public class GasPayActivity extends CustomerActionBarActivity implements View.OnClickListener,
        GasPayPopupWindow.OnPayMethodListener {

    @Bind(R.id.et_number)
    EditText mEt_number;
    @Bind(R.id.et_money)
    EditText mEt_money;
    @Bind(R.id.btn_next)
    Button mNext;
    private GasPayPopupWindow popupWindow;
    private Handler mHandler = new MyHandler();

    public static Intent newIntent() {
        Intent intent = new Intent(GasApplication.getInstance(), GasPayActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_gas_pay);
        ButterKnife.bind(this);
        initEvents();
    }

    private void initEvents() {
        popupWindow = new GasPayPopupWindow(this, 0.9f, 0);
        popupWindow.setPayMethodListener(this);
        popupWindow.setAnimationStyle(R.style.anim_popup_from_bottom);
        mNext.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_next:
                if (checkData()) {
                    Util.closeSoftInput(this);
                    popupWindow.showAtLocation(null, Gravity.BOTTOM, 0, 0);
                }
                break;
        }
    }

    private boolean checkData() {
        String num = mEt_number.getText().toString().trim();
        String money = mEt_money.getText().toString().trim();
        if (TextUtils.isEmpty(num)) {
            ToastUtil.showToast(GasPayActivity.this, "请输入客户编号");
            return false;
        }
        if (TextUtils.isEmpty(money)) {
            ToastUtil.showToast(GasPayActivity.this, "请输入充值金额");
            return false;
        }
        try {
            Float.parseFloat(money);
        } catch (NumberFormatException e) {
            ToastUtil.showToast(GasPayActivity.this, "请输入正确的金额");
            return false;
        }
        return true;
    }

    @Override
    public void onAliClicked() {
        PayUtils.payByAli(this, mEt_number.getText().toString().trim(), "燃气缴费", "燃气缴费", mEt_money
                .getText().toString().trim(), mHandler);
    }

    @Override
    public void onWxClicked() {
        PayUtils.payByWx(this,mEt_number.getText().toString().trim(),mEt_money.getText().toString().trim(),"燃气缴费");
    }

    @Override
    public void onEvent(MessageEvent event) {
        super.onEvent(event);
        if(MessageEvent.MessageType.ACTIVITY_FINISH == event.messageType) {
            finish();
        }
    }

    static class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            {
                switch (msg.what) {
                    case PayUtils.SDK_PAY_FLAG: {
                        PayResult payResult = new PayResult((String) msg.obj);
                        // 支付宝返回此次支付结果及加签，建议对支付宝签名信息拿签约时支付宝提供的公钥做验签
                        String resultInfo = payResult.getResult();
                        String resultStatus = payResult.getResultStatus();
                        // 判断resultStatus 为“9000”则代表支付成功，具体状态码代表含义可参考接口文档
                        Intent intent = null;
                        if (TextUtils.equals(resultStatus, "9000")) {
                            intent = GasPayResultActivity.startSuccessInstance();
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            GasApplication.getInstance().startActivity(intent);
                        } else {
                            // 判断resultStatus 为非“9000”则代表可能支付失败
                            // “8000”代表支付结果因为支付渠道原因或者系统原因还在等待支付结果确认，最终交易是否成功以服务端异步通知为准（小概率状态）
                            if (TextUtils.equals(resultStatus, "8000")) {
                                ToastUtil.showToast(GasApplication.getInstance(), "支付结果确认中");
                            } else {
                                // 其他值就可以判断为支付失败，包括用户主动取消支付，或者系统返回的错误
                                intent = GasPayResultActivity.startFailedInstance();
                                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                GasApplication.getInstance().startActivity(intent);
                            }
                        }
                        EventBus.getDefault().post(new MessageEvent(MessageEvent.MessageType.ACTIVITY_FINISH));
                        break;
                    }
                    case PayUtils.SDK_CHECK_FLAG: {
                        LogUtil.showInfoLog("result = " + msg.obj);
                    }
                    break;
                    default:
                        break;
                }
            }
        }
    }
}
